package com.ibeetl.job;


import com.ibeetl.cms.entity.XxdActivitie;
import com.ibeetl.cms.entity.XxdItem;
import com.ibeetl.cms.service.XxdActivitieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 最佳销售统计
 */
@Component
public class BestItemJob {

    @Autowired
    private XxdActivitieService xxdActivitieService;

    /**
     * 每天5时0分0秒触发
     */
    @Scheduled(cron = "0 0 5 * * ?")
    public void cronStatistics() {
        try {
            List<XxdItem> list = xxdActivitieService.queryBestItems();
            xxdActivitieService.removeBest();
            if(list != null){
                for(int i = 0;i < list.size();i++){
                    if(i >= 4){
                        break;
                    }
                    XxdItem item = list.get(i);
                    XxdActivitie xxdActivitie = new XxdActivitie();
                    xxdActivitie.setName("最佳销售");
                    xxdActivitie.setItemId(item.getId().intValue());
                    xxdActivitie.setActivitieId(3);
                    xxdActivitie.setUpdateTime(new Date());
                    xxdActivitie.setDel("0");
                    xxdActivitie.setStatus("0");
                    xxdActivitieService.save(xxdActivitie);
                }
            }else{
                throw new Exception();
            }
        }catch (Exception e){
            System.out.println("统计失败");
        }
    }
}
